<template>
  <div class="product-list-container">
    <div class="page-header">
      <h2 class="page-title">商品管理</h2>
      <el-button type="primary" @click="$router.push('/product/add')">新增商品</el-button>
    </div>

    <div class="content-container">
      <!-- 搜索区域 -->
      <div class="section-card">
        <el-form :inline="true" :model="searchForm" class="search-form">
          <el-form-item>
            <el-input v-model="searchForm.name" placeholder="商品名称" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchForm.category" placeholder="商品分类" clearable>
              <el-option
                v-for="item in categoryOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchForm.status" placeholder="商品状态" clearable>
              <el-option label="在售" value="在售"></el-option>
              <el-option label="已下架" value="已下架"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 表格区域 -->
      <div class="section-card">
        <el-table :data="tableData" border style="width: 100%" v-loading="loading">
          <el-table-column type="index" label="序号" width="80"></el-table-column>
          <el-table-column prop="name" label="商品名称" min-width="180">
            <template slot-scope="scope">
              <div class="product-info">
                <el-image
                  :src="scope.row.image"
                  :preview-src-list="[scope.row.image]"
                  class="product-image"
                ></el-image>
                <span>{{ scope.row.name }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="category" label="分类" width="120"></el-table-column>
          <el-table-column prop="price" label="价格(积分)" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.price }} 分</span>
            </template>
          </el-table-column>
          <el-table-column label="库存" width="180">
            <template slot-scope="scope">
              <div>
                <div>
                  实际库存:
                  <span style="font-weight: bold">{{ scope.row.stock }}</span>
                </div>
                <div>
                  可用库存:
                  <span style="font-weight: bold">{{ scope.row.availableStock || scope.row.stock }}</span>
                </div>
                <el-button
                  v-if="scope.row.skus && scope.row.skus.length > 0"
                  type="text"
                  size="mini"
                  @click="viewSkus(scope.row)"
                >查看SKU</el-button>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="100">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status === '在售' ? 'success' : 'info'">{{ scope.row.status }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button
                type="text"
                size="small"
                :class="{'delete-btn': true}"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination-container">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getProductList,
  deleteProduct,
  updateProductStatus,
} from '@/api/product'

export default {
  name: 'ProductList',
  data() {
    return {
      searchForm: {
        name: '',
        category: '',
        status: '',
      },
      categoryOptions: [
        { value: '数码', label: '数码' },
        { value: '食品', label: '食品' },
        { value: '服装', label: '服装' },
        { value: '家居', label: '家居' },
      ],
      tableData: [],
      loading: false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      skusDialogVisible: false,
      currentProduct: null,
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.loading = true
      const params = {
        page: this.currentPage,
        limit: this.pageSize,
        ...this.searchForm,
      }

      getProductList(params)
        .then(res => {
          this.tableData = res.data.list
          this.total = res.data.total
        })
        .catch(error => {
          console.error('获取商品列表失败', error)
          this.$message.error('获取商品列表失败')
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleSearch() {
      this.currentPage = 1
      this.fetchData()
    },
    resetSearch() {
      this.searchForm = {
        name: '',
        category: '',
        status: '',
      }
      this.handleSearch()
    },
    handleEdit(row) {
      this.$router.push(`/product/add?id=${row.id}`)
    },
    handleDelete(row) {
      this.$confirm('确认删除该商品吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.loading = true
          deleteProduct(row.id)
            .then(() => {
              this.$message.success('删除成功')
              this.fetchData()
            })
            .catch(error => {
              console.error('删除商品失败', error)
              this.$message.error('删除商品失败')
            })
            .finally(() => {
              this.loading = false
            })
        })
        .catch(() => {})
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    viewSkus(product) {
      this.currentProduct = product
      this.$router.push(`/product/add?id=${product.id}`)
    },
  },
}
</script>

<style scoped>
.product-list-container {
  padding: 24px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding: 24px;
  background: #ffffff;
  border-radius: 8px;
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0;
}

.content-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.section-card {
  background: #ffffff;
  padding: 24px;
  border-radius: 8px;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.search-form .el-form-item {
  margin-bottom: 0;
}

.product-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.product-image {
  width: 40px;
  height: 40px;
  border-radius: 4px;
  object-fit: cover;
}

.pagination-container {
  margin-top: 24px;
  display: flex;
  justify-content: flex-end;
}

.delete-btn {
  color: #f53f3f;
}

.delete-btn:hover {
  color: #f76560;
}

/* 表格样式优化 */
.el-table th {
  background-color: #f7f7f7;
  color: #4d4d4d;
  font-weight: 600;
  height: 48px;
}

.el-table td {
  color: #666666;
  height: 56px;
}
</style> 